<template>
    <div v-loading="loading">
        <el-row :gutter="30" v-for="(corpl,index) in corpPartnerList" :key="index" style="margin-top: 10px;margin-right: -10px;">
            <el-col :span="8" v-for="(corpinfo,innerindex) in corpl" :key="innerindex" @click="clickJump(corpinfo.corpid)">
                <el-badge v-if="corpBadge[corpinfo.corpid] != undefined && corpBadge[corpinfo.corpid].isclick == 0" :value="corpBadge[corpinfo.corpid].count" class="item grid-content">
                    <el-card class="box-card groupcard">
                        <el-row align="middle">
                            <el-col :span="20">
                                <p class="grouptitle txtclamp" :title="corpinfo.corpname">{{corpinfo.corpname}}</p>
                                <p>媒资：{{corpinfo.mediacount}}</p>
                                <p>最后更新时间：{{corpinfo.lasttime}}</p>
                            </el-col>
                            <el-col :span="4">
                                <i class="iconfont icon-zhuomian"></i>
                            </el-col>
                        </el-row>
                        <!-- <el-row class="groupbottom">
                            <el-col :span="9">媒资：{{groupinfo.mediacount}}</el-col>
                            <el-col :span="15" align="right">最后更新时间：2021-12-24 15:11</el-col>
                        </el-row> -->
                    </el-card>
                </el-badge>
                <el-badge v-else class="item grid-content">
                    <el-card class="box-card groupcard">
                        <el-row align="middle">
                            <el-col :span="20">
                                <p class="grouptitle txtclamp" :title="corpinfo.corpname">{{corpinfo.corpname}}</p>
                                <p>媒资：{{corpinfo.mediacount}}</p>
                                <p>最后更新时间：{{corpinfo.lasttime}}</p>
                            </el-col>
                            <el-col :span="4">
                                <i class="iconfont icon-zhuomian"></i>
                            </el-col>
                        </el-row>
                    </el-card>
                </el-badge>
            </el-col>
        </el-row>
        <table-pagination
        :queryList="queryList"
        :total="corpPartnerTotal"
        @pagination="getCorpPartnerList"
        />
    </div>
</template>

<script>
    import {useStore} from 'vuex'
    import { useRouter } from 'vue-router';
    import Cookies from 'js-cookie'
    import { ref } from 'vue'
    import { getCorpPartner, getMediaShareStatistics } from '@/api/corppartner/index'
    import CookiesDomainMixin from "@/mixins/cookiesDomainMixin";
    import TablePagination from "@/views/public/TablePagination";
    export default {
        name: "",
        components: {
            TablePagination
        },
        setup() {
            const store = useStore()
            const corpid = store.getters.corpinfo.corpid
            const corpBadge = ref({})
            const loading = ref(true)
            const { vcloudDomain } = CookiesDomainMixin()

            /* 获取互信企业---start */
            const corpPartnerList = ref([])
            const queryList = ref({pagesize:10,corpid:corpid,partnertype:0})
            const corpPartnerTotal = ref(0)
            async function getCorpPartnerList() {
                corpPartnerList.value = []
                let firstCorpInfo = {
                    "corpid": 0,
                    "corpname": "对外分享的全部媒资",
                    "mediacount": '正在加载...',
                    "lasttime": '正在加载...'
                }
                corpPartnerList.value[0] = []
                corpPartnerList.value[0].push(firstCorpInfo)
                getCorpPartner(queryList.value).then(result => {
                    loading.value = false
                    if( result.errcode == 0 && result.data.list.length > 0 ){
                        corpPartnerTotal.value = result.data.total
                        result.data.list.forEach(function(value,index){
                            const cindex = Math.floor((index+1)/3)
                            if( !corpPartnerList.value[cindex] ){
                                corpPartnerList.value[cindex] = []
                            }
                            value.mediacount = '正在加载...'
                            value.lasttime = '正在加载...'
                            corpPartnerList.value[cindex].push(value)
                        })
                        getCorpPartnerStatistics()
                    }else{
                        console.log(result)
                    }
                })
            }
            /* 获取互信企业---end */

            /* 获取互信企业统计信息--start */
            function getCorpPartnerStatistics( ){
                if( corpPartnerList.value && corpPartnerList.value.length > 0 ){
                    // 获取本地数据---start
                    let localStorageKey = corpid + '_2'
                    let localStorageData = {}
                    if( window.localStorage ){
                        let myLocalStorage = window.localStorage
                        
                        if( myLocalStorage[localStorageKey] != undefined ){
                            localStorageData = JSON.parse(myLocalStorage[localStorageKey])
                        }
                    }else{
                        if( Cookies.get(localStorageKey) != undefined ){
                            localStorageData = JSON.parse(Cookies.get(localStorageKey))
                        }
                    }
                    // 获取本地数据---end
                    let promiseData = []
                    corpPartnerList.value.forEach(function( corpPartnerRowData ){
                        if( corpPartnerRowData.length && corpPartnerRowData.length > 0 ){
                            corpPartnerRowData.forEach(function( corpPartnerInfo ){
                                let query={type:1}
                                if( corpPartnerInfo.corpid != 0 ){
                                    query.partnercorpid = corpPartnerInfo.corpid
                                }
                                promiseData.push(getMediaShareStatistics( query ))
                            })
                        }
                    })
                    if( promiseData.length > 0 ){
                        Promise.all(promiseData.map(p => p.catch(e => {return e}))).then((result) => {
                            let promiseAllRes = result
                            let promiseAllResKey = 0
                            corpPartnerList.value.forEach(function( corpPartnerRowData ){
                                if( corpPartnerRowData.length && corpPartnerRowData.length > 0 ){
                                    corpPartnerRowData.forEach(function( corpPartnerInfo ){
                                        // console.log(corpPartnerInfo)
                                        if( promiseAllRes[promiseAllResKey] != undefined && promiseAllRes[promiseAllResKey].errcode == 0 ){
                                            corpPartnerInfo.mediacount = promiseAllRes[promiseAllResKey].data.mediacount
                                            if( promiseAllRes[promiseAllResKey].data.mts > 0 ){
                                                corpPartnerInfo.lasttime = formartDate(promiseAllRes[promiseAllResKey].data.mts*1000)
                                            }else{
                                                corpPartnerInfo.lasttime = '无'
                                            }
                                        }else{
                                            console.log(promiseAllRes[promiseAllResKey])
                                        }
                                        promiseAllResKey++
                                        if( localStorageData[corpPartnerInfo.corpid] != undefined ){
                                            if( corpPartnerInfo.mediacount > localStorageData[corpPartnerInfo.corpid].oldcount ){
                                                localStorageData[corpPartnerInfo.corpid] = {
                                                    'count':(corpPartnerInfo.mediacount-localStorageData[corpPartnerInfo.corpid].oldcount),
                                                    'oldcount':localStorageData[corpPartnerInfo.corpid].oldcount,
                                                    'newcount':corpPartnerInfo.mediacount,
                                                    'isclick':0
                                                }
                                                corpBadge.value[corpPartnerInfo.corpid] = localStorageData[corpPartnerInfo.corpid]
                                                // localStorageData[groupInfo.groupid] = JSON.stringify(corpBadge.value[groupInfo.groupid])
                                            }else{
                                                localStorageData[corpPartnerInfo.corpid] = {
                                                    'count':0,
                                                    'oldcount':corpPartnerInfo.mediacount,
                                                    'newcount':corpPartnerInfo.mediacount,
                                                    'isclick':1
                                                }
                                                corpBadge.value[corpPartnerInfo.corpid] = localStorageData[corpPartnerInfo.corpid]
                                            }
                                        }else{
                                            localStorageData[corpPartnerInfo.corpid] = {'count':corpPartnerInfo.mediacount,'newcount':corpPartnerInfo.mediacount,'oldcount':corpPartnerInfo.mediacount}
                                            if( corpPartnerInfo.mediacount == 0 ){
                                                localStorageData[corpPartnerInfo.corpid].isclick = 1
                                            }else{
                                                localStorageData[corpPartnerInfo.corpid].isclick = 0
                                            }
                                            corpBadge.value[corpPartnerInfo.corpid] = localStorageData[corpPartnerInfo.corpid]
                                        }
                                    })
                                }
                            })
                            
                            // 写入本地数据---start
                            if( Object.keys(localStorageData).length > 0 ){
                                if( window.localStorage ){
                                    let myLocalStorage = window.localStorage
                                    myLocalStorage[localStorageKey] = JSON.stringify(localStorageData)
                                }else{
                                    Cookies.set(localStorageKey, JSON.stringify(localStorageData), { expires: 365,domain: vcloudDomain })
                                }
                            }
                            // 写入本地数据---end
                        })
                    }
                }
            }

            function formartDate(param) {
                var time = new Date(param);
                var y = time.getFullYear();
                var m = time.getMonth()+1;
                var d = time.getDate();
                var h = time.getHours();
                var mm = time.getMinutes();
                var s = time.getSeconds();
                return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
            }

            function add0(m){
                return m<10?'0'+m:m;
            }
            /* 获取互信企业统计信息--end */

            //点击跳转链接
            const router = useRouter();
            const clickJump = ( partnercorpid ) => {
                let localStorageKey = corpid + '_2'
                let localStorageData = ''
                if( window.localStorage ){
                    let myLocalStorage = window.localStorage
                    localStorageData = myLocalStorage[localStorageKey]
                    if( localStorageData != undefined ){
                        let localData = JSON.parse(localStorageData)
                        if( localData[partnercorpid].isclick != 1 ){
                            localData[partnercorpid].isclick = 1
                            localData[partnercorpid].oldcount = localData[partnercorpid].newcount
                            myLocalStorage[localStorageKey] = JSON.stringify(localData)
                        }
                    }
                }else{
                    localStorageData = Cookies.get(localStorageKey)
                    if( localStorageData != undefined ){
                        let cookieData = JSON.parse(localStorageData)
                        // console.log(cookieData)
                        if( cookieData[partnercorpid].isclick != 1 ){
                            cookieData[partnercorpid].isclick = 1
                            cookieData[partnercorpid].oldcount = cookieData[partnercorpid].newcount
                            Cookies.set(localStorageKey, JSON.stringify(cookieData), { expires: 365,domain: vcloudDomain })
                        }
                    }
                }
                router.push({name:'mediasharesharemedias',query: {partnercorpid:partnercorpid, activename: 'mediasharelist'}})
            }

            const init = () => {
                getCorpPartnerList()
            }
            init()
            return {
                loading,
                corpBadge,
                corpPartnerList,
                queryList,
                corpPartnerTotal,
                getCorpPartnerList,
                clickJump,
                init
            }
        }
    }
</script>

<style scoped lang="scss">
    /* el-row-start */
    .el-row {
    margin-bottom: 20px;
    padding-right: 5px;
    &:last-child {
        margin-bottom: 0;
    }
    }
    .el-col {
    border-radius: 4px;
    }
    .bg-purple-dark {
    background: #99a9bf;
    }
    .bg-purple {
    background: #d3dce6;
    }
    .bg-purple-light {
    background: #e5e9f2;
    }
    .grid-content {
        width:100%;
        border-radius: 4px;
        min-height: 80px;
       // border: #dcdfe6 1px solid;
    }
    .groupcard{
        cursor: pointer;
        p{
            line-height: 22px;
        }
        font-size: 12px;
        color: #333;
        background-color: #fff!important;
        .grouptitle{margin-bottom:10px;font-size: 18px;font-weight: bold;}
        .icon-zhuomian{font-size: 50px;color:rgba(0,0,0,0.15);font-weight: bold;}
    }

    .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
    }
    /* el-row-end */
    
</style>
<style lang="scss">
    .groupcard{
        border:0;
    }
    .el-tabs{
        .el-tabs__content{
            overflow: unset;
        }
    }
</style>
